<template>
  <div class="ds-left">
    <div class="eq-left-top" style="display:none">
      <div class="eq-left-top-title">
        <img src="../../../assets/images/home-page/eq-right.png" alt="" />
        设备统计
      </div>
      <div class="eq-left-top-content">
        <div class="zsb-box">
          <div class="eq-r-t-i-title">
            <span class="eq-dio"></span>
            <span class="eq-d-title">主设备</span>
          </div>
          <div class="zse-item">
            <div class="z-i-top">
              <img src="../../../assets/images/home-page/sb-t.png" alt="" />
              <span>今日告警次数</span>
            </div>
            <div class="z-i-b">
              <div> </div>
              <div class="z-b-left">
                <span class="z-b-span">{{ obgj.todaySum }}</span>
                <div class="z-b-div">
                  <span class="z-b-div-zr">较昨日</span>
                  <span class="z-b-div-sz">{{ obgj.minus }}</span>
                  <img src="../../../assets/images/home-page/sb-up.png" alt="" v-if="obgj.minus > 0" />
                  <img src="../../../assets/images/home-page/sb-bt.png" alt="" v-if="obgj.minus < 0" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="zsb-box zsb-bt">
          <div class="eq-r-t-i-title">
            <span class="eq-dio"></span>
            <span class="eq-d-title">辅助设备</span>
          </div>
          <div class="zse-item">
            <div class="z-i-top">
              <img src="../../../assets/images/home-page/sb-b.png" alt="" />
              <span>今日告警次数</span>
            </div>
            <div class="z-i-b z-i-bt">
              <div> </div>
              <div class="z-b-left">
                <span class="z-b-span">{{ obgjFu.todaySum }}</span>
                <div class="z-b-div">
                  <span class="z-b-div-zr">较昨日</span>
                  <span class="z-b-div-sz">{{ obgjFu.minus }}</span>
                  <img src="../../../assets/images/home-page/sb-up.png" alt="" v-if="obgjFu.minus > 0" />
                  <img src="../../../assets/images/home-page/sb-bt.png" alt="" v-if="obgjFu.minus < 0" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="chart-container">
          <div ref="pieChartRef" class="pie-chart"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { getCountByClassify, getAdjunctDevicAlarm } from '@/api/auth/ledger.js';
  import { onMounted } from 'vue';
  import {} from '../../../utils/ue.js';
  import bottom from '@/assets/icon/bottom.png';
  import * as echarts from 'echarts';
  const chartRef = ref(null);
  const myChart = ref(null);
  const option = ref(null);
  const treeList = ref([]);
  const obgj = ref({});
  const obgjFu = ref({});
  const seriesData = ref([]);
  const legendData = ref([]);
  const pieChartRef = ref(null);
  const emit = defineEmits();
  onMounted(() => {
    getTreeList();
  });

  const chartDomFun = () => {
    const myChart = echarts.init(pieChartRef.value);
    const option = {
      tooltip: {
        trigger: 'item',
        formatter: '{b} : {c}次'
      },
      legend: {
        top: '5%',
        left: 'center',
        // 修改图例字体颜色
        textStyle: {
          color: '#fff' // 图例字体颜色设置为白色
        },
        data: legendData.value,
        // 使用formatter函数自定义图例项的文本
        formatter: function (name) {
          // 根据图例名称从series数据中找到对应的value
          const seriesData = option.series[0].data;
          const dataItem = seriesData.find((data) => data.name === name);
          return dataItem ? name + ' ' + dataItem.value + '次' : name;
        }
      },
      series: [
        {
          name: '次数',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          padAngle: 5,
          itemStyle: {
            borderRadius: 10
          },
          label: {
            // 修改图表中间位置的字体颜色
            color: '#ff0000', // 指定颜色，这里使用红色作为示例
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: false,
              fontSize: 40,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: seriesData.value
        }
      ]
    };
    // option.legend.data = option.series.map((item) => item.name);
    // option.legend.formatter = function (name) {
    //   // 找到与图例项名称匹配的系列数据
    //   const dataItem = option.series.find((data) => data.name === name);
    //   // 返回自定义的文本内容，包括名称和值
    //   return dataItem ? name + ' ' + dataItem.value + '次' : name;
    // };
    myChart.setOption(option);
  };
  const getTreeList = () => {
    getCountByClassify({ deviceClassify: 0 }).then((res) => {
      obgj.value = res.data;
    });
    getCountByClassify({ deviceClassify: 1 }).then((res) => {
      obgjFu.value = res.data;
    });
    getAdjunctDevicAlarm().then((res) => {
      seriesData.value = res.data.infos.map((element) => {
        return {
          name: element.deviceClassify, // 将 deviceClassify 重命名为 name
          value: element.count // 将 count 重命名为 value
        };
      });
      legendData.value = res.data.infos.map((element) => element.deviceClassify);
      chartDomFun();
    });
  };
</script>

<style lang="scss" scoped>
  .ds-left {
    width: 360px;
    position: absolute;
    top: 120px;
    left: 18px;
    z-index: 999;
    .eq-left-top {
      width: 100%;
      .eq-left-top-title {
        width: 100%;
        height: 33px;
        background: rgba(10, 39, 82, 1);
        display: flex;
        align-items: center;
        color: #fff;
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 16px;
        img {
          width: 20px;
          height: 21px;
          margin: 0 5px 0 10px;
        }
      }
      .eq-left-top-content {
        padding: 10px 0;
        height: 79.5vh;
        background: rgba(5, 30, 67, 0.6);
        .zsb-box {
          width: 327px;
          margin: 0 auto;
          .eq-r-t-i-title {
            width: 100px;
            display: flex;
            align-items: center;
            .eq-dio {
              width: 8px;
              height: 8px;
              background: #19db3c;
              border-radius: 50%;
            }
            .eq-d-title {
              font-family: PingFang SC;
              font-weight: bold;
              font-size: 14px;
              color: #ffffff;
              text-shadow: 0px 2px 3px rgba(0, 0, 0, 0.5);
              margin-left: 5px;
            }
          }
          .zse-item {
            // width: 100%;
            margin-left: 20px;
            margin-top: 30px;
            .z-i-top {
              width: 50%;
              display: flex;
              align-items: center;
              // justify-content: space-between;
              margin-left: 30px;
              margin-bottom: -30px;
              img {
                width: 46px;
                height: 46px;
              }
              span {
                font-family: PingFang SC;
                font-size: 12px;
                color: #ffffff;
                margin-left: 20px;
              }
            }
            .z-i-b {
              width: 233px;
              height: 69px;
              background: url('../../../assets/images/home-page/zsb.png');
              background-size: 100 100%;
              display: flex;
              align-items: center;
              justify-content: space-around;
              .z-b-left {
                display: flex;
                align-items: center;
                justify-content: space-around;
                width: 40%;
                .z-b-span {
                  font-family: DIN;
                  font-weight: bold;
                  font-size: 21px;
                  color: #ffffff;
                  background: linear-gradient(0deg, #54c8e8 0%, #ffffff 100%);
                  -webkit-background-clip: text;
                  -webkit-text-fill-color: transparent;
                }
                .z-b-div {
                  display: flex;
                  align-items: center;
                  .z-b-div-zr {
                    font-family: PingFang SC;
                    font-size: 12px;
                    color: #ffffff;
                  }
                  .z-b-div-sz {
                    font-family: DIN;
                    font-size: 10px;
                    color: #f2634f;
                  }
                  img {
                    width: 15px;
                    height: 17px;
                  }
                }
              }
            }
            .z-i-bt {
              width: 233px;
              height: 69px;
              background: url('../../../assets/images/home-page/zsb-1.png');
              background-size: 100 100%;
              display: flex;
              align-items: center;
              justify-content: space-around;
              .z-b-left {
                display: flex;
                align-items: center;
                justify-content: space-around;
                width: 40%;
                .z-b-span {
                  font-family: DIN;
                  font-weight: bold;
                  font-size: 32px;
                  color: #ffffff;
                  text-shadow: 0px 3px 7px rgba(0, 67, 95, 0.35);
                  background: linear-gradient(0deg, #ffc148 0%, #ffffff 100%);
                  -webkit-background-clip: text;
                  -webkit-text-fill-color: transparent;
                }
                .z-b-div {
                  .z-b-div-zr {
                    font-family: PingFang SC;
                    font-size: 12px;
                    color: #ffffff;
                  }
                  .z-b-div-sz {
                    font-family: DIN;
                    font-size: 10px;
                    color: #66bb83;
                  }
                }
              }
            }
          }
        }
        .zsb-bt {
          margin-top: 50px;
        }
        .chart-container {
          .pie-chart {
            width: 350px;
            height: 450px;
          }
        }
      }
    }
  }
  :deep(.el-tree) {
    background-color: transparent !important;
    height: 71vh;
    overflow-y: scroll;
    .el-tree-node__label {
      font-size: 12px;
      font-family:
        PingFangSC,
        PingFang SC;
      color: #ffffff;
    }
  }
  :deep(.el-tree-node__content:hover) {
    // width: 280px !important;
    // background-color: transparent !important;
    // color: red !important;
    // height: 30px !important;
    background-color: transparent !important;
    // height: 30px !important;
    background-image: url('../../../assets/images/home-page/tree-bg.png') !important;
    background-size: 100% 100% !important;
  }
  :deep(.el-tree-node__expand-icon) {
    color: #ffffff !important;
  }

  :deep(.el-tree-node:focus > .el-tree-node__content) {
    /* background-color: #214a37 !important; */
    background-color: transparent !important;
    // height: 30px !important;
    background-image: url('../../../assets/images/home-page/tree-bg.png') !important;
    background-size: 100% 100% !important;
  }
  // :deep(.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content) {
  //   background-color: transparent !important;
  // }
  /* 如果你想要改变特定节点的背景色 */
  // .eq-left-top-c-tree .el-tree .el-tree-node.is-expanded {
  //   background-color: #f0f0f0; /* 展开节点的颜色 */
  // }
</style>
